package algorithm.leetcode.simple;

/*
 给定一个排序链表，删除所有重复的元素，使得每个元素只出现一次。
 */

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020/10/9
 */
public class Question83 {

    public static void main(String[] args) {

        Question83 question = new Question83();
        ListNode head = new ListNode(1);
        head.next = new ListNode(1);
        question.deleteDuplicates(head);
        System.out.println(head);

    }

    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode curNode = head;
        while (curNode.next != null) {
           if (curNode.next.val == curNode.val){
               curNode.next = curNode.next.next;
               continue;
           }
           curNode = curNode.next;
        }
        return head;
    }

    private static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }

        @Override
        public String toString() {
            return "ListNode{" +
                    "val=" + val +
                    ", next=" + next +
                    '}';
        }
    }
}


